﻿#region
/* ===============================================
 * 作者：guopeng           时间：2012/3/20 22:37:43
 * 
 * 
 * 类描述：
 * 
 * 
 * =============================================== */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ueEngine.Biz.Inf.Entities;
using ueEngine.Biz.Inf.LogicEntity.SupportData;
using ueEngine.Biz.Inf.Entities.DBSupport;
using ueEngine.Framework.Exception;


namespace ueEngine.Biz.Inf.LogicEntity
{
    public class MemberModel : IAccessible<Member,MemberModel>
    {
        #region self
        public int Id { get; set; }
        public string UserName { get; set; }

        public string Password { get; set; }

        /// <summary>
        /// 昵称，非必须
        /// </summary>
        public string NickName { get; set; }

        /// <summary>
        /// 是否已经激活
        /// </summary>
        public bool IsActivate { get; set; }

        /// <summary>
        /// Sha1加密的Guid标示
        /// </summary>
        public string Token { get; set; }

        /// <summary>
        /// Token值的生成时间
        /// </summary>
        public DateTime TokenDate { get; set; }

        /// <summary>
        /// 真实姓名
        /// </summary>
        public string RealName { get; set; }

        /// <summary>
        /// 性别
        /// 
        /// </summary>
        public Gender Sex { get; set; }

        /// <summary>
        /// 现所在区域
        /// </summary>
        public string Location { get; set; }
       
        /// <summary>
        /// 所在学校
        /// </summary>
        public Collage Collage { get; set; }
        /// <summary>
        /// 联系电话
        /// </summary>
        public string Phone { get; set; }

        /// <summary>
        /// 头像
        /// </summary>
        public string Image { get; set; }

        /// <summary>
        /// 角色
        /// </summary>
        public MemberStatus Role { get; set; }

        public string QQ { get; set; }

        public DateTime RegisterDate { get; set; }
        #endregion

        #region corporation
        public CorporationModel CorporationInfo { get; set; }
        #endregion

        protected override void Covariant(Member dbMember)
        {
            this.Id = dbMember.Id;
            this.Image = dbMember.Image;
            this.IsActivate = dbMember.IsActivate;
            this.Location = dbMember.Location;
            this.NickName = dbMember.NickName;
            this.Password = dbMember.Password;
            this.Phone = dbMember.Phone;
            this.RealName = dbMember.RealName;
            this.Role = dbMember.Role ? MemberStatus.Firm : MemberStatus.Seeker;
            this.Sex = dbMember.Sex ? Gender.Male : Gender.Female;
            this.Token = dbMember.Token;
            this.TokenDate = dbMember.TokenDate;
            this.UserName = dbMember.UserName;
            this.RegisterDate = dbMember.RegisterDate;
            this.Collage = (Collage)dbMember.Collage;
            this.QQ = dbMember.QQ;
        }
        protected override Member Inverter(MemberModel model)
        {
            return new Member()
            {
                Id = model.Id,
                Image = model.Image,
                IsActivate = model.IsActivate,
                Location = model.Location,
                NickName = model.NickName,
                Password = model.Password,
                Phone = model.Phone,
                RealName = model.RealName,
                Role = model.Role == MemberStatus.Firm ? true : false,
                Sex = model.Sex == Gender.Male ? true : false,
                Token = model.Token,
                TokenDate = model.TokenDate,
                UserName = model.UserName,
                RegisterDate = model.RegisterDate,
                Collage = (int)model.Collage,
                QQ = model.QQ
            };
        }
        public bool IsExists(string username)
        {
            using (Context ctx = new Context())
            {
                return ctx.Member.Any(c => c.UserName == username);
            }
        }
        //public override MemberModel Element(System.Linq.Expressions.Expression<Func<Member, bool>> where)
        //{
        //    var user = new Member().Get(where);
        //    if (user == null)
        //    {
        //        throw new ContextException("指定会员不存在", ExceptionType.OutOfDataBase, ContextExceptionCode.UserIsNotFound);
        //    }
        //    Covariant(user);
        //    return this;
        //}
        //public override List<TResult> Elements<TResult>(System.Linq.Expressions.Expression<Func<Member, bool>> where, System.Linq.Expressions.Expression<Func<Member, TResult>> select)
        //{
        //    using (Context ctx = new Context())
        //    {
        //        return ctx.Member.Where(where).Select(select).ToList();
        //    }
        //}
    }
}
